package com.abvnet.project.water.api.web;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.framework.shiro.service.PasswordService;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.system.user.domain.User;
import com.abvnet.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ApiUserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private PasswordService passwordService;

    @GetMapping("/beat")
    public AjaxResult get() {
        return success();
    }

    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(Long userId, String oldPassword, String newPassword) {
        User user = userService.selectUserById(userId);
        if (user == null) {
            return error("用户不存在，请联系管理员");
        }
        if (StringUtils.isNotEmpty(newPassword) && passwordService.matches(user, oldPassword)) {
            user.setPassword(newPassword);
            if (userService.resetUserPwd(user) > 0) {
                return success();
            }
            return error();
        } else {
            return error("修改密码失败，旧密码错误");
        }
    }
}